package org.dromara.trace.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.trace.domain.bo.TraceSellproBo;
import org.dromara.trace.domain.vo.TraceSellproVo;
import org.dromara.trace.domain.TraceSellpro;
import org.dromara.trace.mapper.TraceSellproMapper;
import org.dromara.trace.service.ITraceSellproService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 溯源产品Service业务层处理
 *
 * @author Robin
 * @date 2024-12-05
 */
@RequiredArgsConstructor
@Service
public class TraceSellproServiceImpl implements ITraceSellproService {

    private final TraceSellproMapper baseMapper;

    /**
     * 查询溯源产品
     *
     * @param sellproId 主键
     * @return 溯源产品
     */
    @Override
    public TraceSellproVo queryById(Long sellproId){
        return baseMapper.selectVoById(sellproId);
    }

    /**
     * 分页查询溯源产品列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 溯源产品分页列表
     */
    @Override
    public TableDataInfo<TraceSellproVo> queryPageList(TraceSellproBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TraceSellpro> lqw = buildQueryWrapper(bo);
        Page<TraceSellproVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的溯源产品列表
     *
     * @param bo 查询条件
     * @return 溯源产品列表
     */
    @Override
    public List<TraceSellproVo> queryList(TraceSellproBo bo) {
        LambdaQueryWrapper<TraceSellpro> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TraceSellpro> buildQueryWrapper(TraceSellproBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TraceSellpro> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getSellproName()), TraceSellpro::getSellproName, bo.getSellproName());
        lqw.eq(StringUtils.isNotBlank(bo.getSellproArea()), TraceSellpro::getSellproArea, bo.getSellproArea());
        lqw.eq(StringUtils.isNotBlank(bo.getSellproWeight()), TraceSellpro::getSellproWeight, bo.getSellproWeight());
        lqw.eq(StringUtils.isNotBlank(bo.getSellproGuige()), TraceSellpro::getSellproGuige, bo.getSellproGuige());
        lqw.eq(StringUtils.isNotBlank(bo.getSellproImg()), TraceSellpro::getSellproImg, bo.getSellproImg());
        lqw.eq(bo.getTemplateId() != null, TraceSellpro::getTemplateId, bo.getTemplateId());
        lqw.eq(bo.getCodeTemplateId() != null, TraceSellpro::getCodeTemplateId, bo.getCodeTemplateId());
        lqw.eq(StringUtils.isNotBlank(bo.getGermplasmId()), TraceSellpro::getGermplasmId, bo.getGermplasmId());
        lqw.eq(StringUtils.isNotBlank(bo.getBatchId()), TraceSellpro::getBatchId, bo.getBatchId());
        lqw.eq(StringUtils.isNotBlank(bo.getLandId()), TraceSellpro::getLandId, bo.getLandId());
        lqw.eq(StringUtils.isNotBlank(bo.getDeviceId()), TraceSellpro::getDeviceId, bo.getDeviceId());
        lqw.eq(StringUtils.isNotBlank(bo.getCameraId()), TraceSellpro::getCameraId, bo.getCameraId());
        lqw.eq(StringUtils.isNotBlank(bo.getStaffId()), TraceSellpro::getStaffId, bo.getStaffId());
        lqw.eq(StringUtils.isNotBlank(bo.getShopId()), TraceSellpro::getShopId, bo.getShopId());
        lqw.eq(StringUtils.isNotBlank(bo.getH5Url()), TraceSellpro::getH5Url, bo.getH5Url());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), TraceSellpro::getStatus, bo.getStatus());
        lqw.eq(bo.getOrderNum() != null, TraceSellpro::getOrderNum, bo.getOrderNum());
        lqw.eq(bo.getBaseId() != null, TraceSellpro::getBaseId, bo.getBaseId());
        lqw.eq(bo.getDeptId() != null, TraceSellpro::getDeptId, bo.getDeptId());
        lqw.eq(bo.getUserId() != null, TraceSellpro::getUserId, bo.getUserId());
        return lqw;
    }

    /**
     * 新增溯源产品
     *
     * @param bo 溯源产品
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(TraceSellproBo bo) {
        TraceSellpro add = MapstructUtils.convert(bo, TraceSellpro.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setSellproId(add.getSellproId());
        }
        return flag;
    }

    /**
     * 修改溯源产品
     *
     * @param bo 溯源产品
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(TraceSellproBo bo) {
        TraceSellpro update = MapstructUtils.convert(bo, TraceSellpro.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TraceSellpro entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除溯源产品信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
